package com.mall.shopping.services;

import com.mall.shopping.IProductService;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ShoppingGoodsServiceImpl implements IProductService {
    /**
     * 查看商品明细
     *
     * @param request
     * @return
     */
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        return null;
    }

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    ContentConverter contentConverter;
    /**
     * 查询所有商品（分页）
     *
     * @param request
     * @return
     */
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        //找到所有符合条件的PO实体
        List<Item> items = itemMapper.selectItemByAllProductRequest(request.getCid(),request.getSize(),request.getPage(),request.getSort(),request.getPriceGt(),request.getPriceLte());
        //利用转换器一个个转成DTO，并且封装成Response
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        long size = productDtos.size();
        AllProductResponse allProductResponse = new AllProductResponse(productDtos, size);
        return allProductResponse;
    }

    /**
     * 获取推荐的商品板块
     *
     * @return
     */
    @Override
    public RecommendResponse getRecommendGoods() {
        //先查询PanelContentItemDto对应的PO，封装好，赋值到PanelDto的成员变量中
        RecommendResponse recommendResponse = new RecommendResponse();
        //先去获取panelDto
        List<Panel> panels = panelMapper.selectAll();
        List<PanelDto> panelDtos = contentConverter.panel2Dto(panels);
        for (PanelDto p:panelDtos
             ) {
            Integer id = p.getId();
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(id);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            p.setPanelContentItems(panelContentItemDtos);
        }
        Set<PanelDto> panelDtos1 = new HashSet<>();
        for (PanelDto pd:panelDtos
             ) {
            panelDtos1.add(pd);
        }
        recommendResponse.setPanelContentItemDtos(panelDtos1);
        return recommendResponse;
    }
}
